<?php

namespace Modules\ShopAdmin\Models;
use Modules\AuthAdmin\Services\QueryService;
class ShopProductAttrbuteValue extends BaseApiModel
{
	/**
	 * @name  关联商品属性表   多对一
	 */
	 public function attrbute_key_to()
    {
        return $this->belongsTo('Modules\ShopAdmin\Models\ShopProductAttrbuteKey','attrbute_key_id','id');
    }
	/**
     * @name   状态0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getStatusAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
     * @name   更新时间为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value?$value:'';
    }
	/**
	 * @name  列表数据
	 * @param  data  array 搜索数据
	 * @return json 
	 */
    public function list_data($data){
		$model = $this->query();
		$model = (new QueryService())->query_condition($model,$data,'value');
		if(isset($data['attrbute_key_id']) && $data['attrbute_key_id']>0){
			$model = $model->where('attrbute_key_id',$data['attrbute_key_id']);
		}	
        $list = $model->select('id','value','attrbute_key_id','status','created_at','updated_at','sort')
				->with([
					'attrbute_key_to'=>function($query){
							$query->select('id','name');  
					}
				])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->paginate($data['pagesize'])
				->toArray(); 
		return $this->api_success(['data'=>[
			'list'=>$list['data'],
			'total'=>$list['total']
		]]);		
	}
}
